import {StyleSheet, View} from "react-native";
import PrimaryButton from "@/app/components/Button";
import {StatusBar} from "expo-status-bar";
import * as FileSystem from 'expo-file-system';
export function SelectFile(){
    // return filePath.split('.').pop() || '';
    let a  = FileSystem.documentDirectory;
    return(
        <View style={styles.container}>
            <View style={styles.footerContainer}>
                <PrimaryButton label="Choose a File"
                               onPress={() => {alert("File Name: "+a)}}></PrimaryButton>
            </View>
            <StatusBar style="auto"/>
        </View>
    )
}

export function SelectFile2({fileName}: {fileName: string}){
    // return filePath.split('.').pop() || '';
    console.log("fileName: " + fileName.toString());
    return(
        <View style={styles.container}>
            <View style={styles.footerContainer}>
                <PrimaryButton label="Choose a File"
                               onPress={() => {alert("File Name: " + fileName)}}></PrimaryButton>
            </View>
            <StatusBar style="auto"/>
        </View>
    )
}
//
export async function readTextFile(filePath:any) {
    try {
        // 检查文件是否存在
        console.log("filePath: " + filePath)
        const fileInfo = await FileSystem.getInfoAsync(filePath);
        if (!fileInfo.exists) {
            console.log(`File does not exist at path: ${filePath}`);
            return null;
        }

        // 读取文件内容
        const content = await FileSystem.readAsStringAsync(filePath);
        console.log('File content:', content);
        return content;
    } catch (error) {
        console.error('Error reading file:', error);
        throw error;
    }
}
//
// // 使用函数读取文件
// const filePath = FileSystem.documentDirectory + 'example.txt';
// readTextFile(filePath).then(content => {
//     // 处理文件内容
// }).catch(error => {
//     // 处理错误
// });
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#25292e',
        alignItems: 'center',
        justifyContent: 'center',
    },
    imageContainer: {
        flex: 1,
        paddingTop: 58,
    },
    footerContainer: {
        flex: 1 / 3,
        alignItems: 'center',
    },
});
